<?php

/**
 * categoria actions.
 *
 * @package    Terapia Vital
 * @subpackage categoria
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class categoriaActions extends sfActionsFoxsis
{
	public function executeIndex(sfWebRequest $request)
	{
		return $this->forward('categoria', 'list');
	}

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= CategoriaTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE, 'categoria/index');

		sfLoader::loadHelpers(array('Tag','Url'));

		foreach($this->data as $key => $r)
		{
			$actions = array(
				'edit'	 => link_to(
	    			' ',
			url_for($this->getModuleName().'/edit?id_categoria='.$r['id_categoria']),
			array(
	    				'class' => 'icoEditar'
	    				)
	    				),
				'delete' => link_to(
	    			' ',
	    				url_for($this->getModuleName().'/delete?id_categoria='.$r['id_categoria']),
	    				array(
	    				'class' => 'icoExcluir delete',
	    				'rel' => url_for($this->getModuleName().'/grid')
	    				)
	    				)
	    				);
	    				$this->data[$key]['act'] = implode(' ', $actions);
		}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


	public function executeList()
	{
		$this->form = new CategoriaForm();
		$this->colunas	= array(
		$this->form['id_categoria']->renderLabel() => "id_categoria",
		$this->form['nm_categoria']->renderLabel() => "nm_categoria",
		$this->form['id_categoria_pai']->renderLabel() => "categoria_pai",
		$this->form['ds_categoria']->renderLabel() => "ds_categoria",
		$this->form['slug']->renderLabel() => "slug",
			"Ações"	=> "act"
			);

			$this->filter = $this->_getFilter();

			$this->setTemplate('index');
	}

	public function executeFilter()
	{
		$filter = new Filtro(
		$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
		sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
		serialize($filter),
		$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new CategoriaForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new CategoriaForm();

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'new');
	}

	public function validateCreate()
	{
		return true;
	}

	public function handleErrorCreate()
	{
		return $this->forward($this->getModuleName(), 'new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($categoria = Doctrine::getTable('Categoria')->find(array($request->getParameter('id_categoria'))), sprintf('Object categoria does not exist (%s).', $request->getParameter('id_categoria')));
		$this->form = new CategoriaForm($categoria);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($categoria = Doctrine::getTable('Categoria')->find(array($request->getParameter('id_categoria'))), sprintf('Object categoria does not exist (%s).', $request->getParameter('id_categoria')));
		$this->form = new CategoriaForm($categoria);

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'edit');
	}

	public function validateUpdate()
	{
		return true;
	}

	public function handleErrorUpdate()
	{
		return $this->forward($this->getModuleName(), 'edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		try
		{
			$request->checkCSRFProtection();

			$this->forward404Unless($categoria = Doctrine::getTable('Categoria')->find(array($request->getParameter('id_categoria'))), sprintf('Object categoria does not exist (%s).', $request->getParameter('id_categoria')));
			$categoria->delete();

			$this->redirect('categoria/index');
		}
		catch(Excepiton $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
			$this->redirect('categoria/index');
		}

	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		try
		{
			$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
			if ($form->isValid())
			{
				$categoria = $form->save();
				$this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
				$this->redirect('categoria/edit?id_categoria='.$categoria->getIdCategoria());
			}
			else
			{
				throw new Exception($form->getErrorSchema());
			}
		}
		catch(Exception $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
		}
	}
}
